package com.isoft.controller.flight;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.isoft.bean.ResponseData;
import com.isoft.controller.BaseController;
import com.isoft.controller.BaseController;
import com.isoft.entity.ball.Blls;
import com.isoft.entity.flight.FlightView;
import com.isoft.service.flight.FlightViewService;
import com.isoft.service.flight.impl.FlightViewServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 航班视图控制层
 */
@RestController
@CrossOrigin
@RequestMapping("/view")
public class FlightViewController extends BaseController {

    @Autowired
    FlightViewServiceImpl flightViewService;

    @GetMapping("/all")
    public ResponseData<Object> getAllFlightView() {
        return returnObject(flightViewService.selectAllFlightView());
    }

    @GetMapping("/find")
    public ResponseData<Object> getFlightViewByFLID(Integer FLID) {
        return returnObject(flightViewService.selectFlightViewByFLID(FLID));
    }

    @PostMapping("/insert")
    public ResponseData<Boolean> insertFlightView(@RequestBody FlightView flightView) {
        Boolean result = flightViewService.insertFlightView(flightView);
//        如果result是真就返回成功的结果，如果是假就返回失败的结果
        if(result){
            return returnBoolean(result, "200", "添加成功");
        }else {
            return returnBoolean(result, "500", "添加失败！该航班号信息已存在");
        }
    }
    @PutMapping("/update")
    public ResponseData<Boolean> updateFlightView(@RequestBody FlightView flightView) {
        Boolean result = flightViewService.updateFlightView(flightView);
        return returnBoolean(result);
    }
    @DeleteMapping("/{ID}")
    public ResponseData<Boolean> deleteFlightView(@PathVariable Integer ID) {
        Boolean result = flightViewService.deleteFlightView(ID);
        return returnBoolean(result);
    }

    @GetMapping("/page")
    public ResponseData<List<FlightView>> getFlightViewPage(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "5") int pageSize) {
        try {
            // 调用服务层获取分页数据
            IPage<FlightView> flightViewPage = flightViewService.getFlighViewtPage(currentPage, pageSize);

            // 从IPage中提取数据列表和总记录数
            List<FlightView> dataList = flightViewPage.getRecords(); // 获取数据列表
            long total = flightViewPage.getTotal(); // 获取总记录数

            // 封装到ResponseData中
            return ResponseData.success(dataList, total);
        } catch (Exception e) {
            // 异常处理，返回错误信息
            return ResponseData.failure("500", "获取FlightView信息失败: " + e.getMessage());
        }
    }
}
